package com.study.happystudy.service.impl;

import com.study.happystudy.mapper.EmotionMapper;
import com.study.happystudy.po.Emotion;
import com.study.happystudy.po.EmotionData;
import com.study.happystudy.tool.ColorChange;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class EmotionServiceImpl {
    @Resource
    EmotionMapper emotionMapper;

    public void insert(int userId, String emotion) {
        try {
            emotionMapper.insert(userId, emotion);
        } catch (Exception e) {
            ColorChange.Red("Fuck: " + e.getMessage());
        }
    }

    public List<Emotion> selectByUserId(int userId) {
        try {
            return emotionMapper.selectByUserId(userId);
        } catch (Exception e) {
            ColorChange.Red("Fuck: " + e.getMessage());
            return null;
        }
    }

    public void updateEmotion(String emotion,int userId,String date){
        try {
            emotionMapper.updateEmotion(emotion,userId,date);
        } catch (Exception e) {
            ColorChange.Red("Fuck: " + e.getMessage());
        }
    }

    public int isExist(int userId,String date){
        try{
            return emotionMapper.isExist(userId,date);
        } catch (Exception e) {
            ColorChange.Red("Fuck: " + e.getMessage());
            return -1;
        }
    }

    public EmotionData getEmotionDataByMonth(int userId, String yearMonth) {
        List<Emotion> emotions = emotionMapper.selectByUserIdAndMonth(userId, yearMonth);
        EmotionData emotionData = new EmotionData();
        emotionData.setYearMonth(yearMonth);

        List<EmotionData.DayEmotion> dayEmotions = emotions.stream().map(emotion -> {
            EmotionData.DayEmotion dayEmotion = new EmotionData.DayEmotion();
            dayEmotion.setDay(Integer.parseInt(emotion.getDate().substring(8, 10)));
            dayEmotion.setEmotion(emotion.getEmotion());
            return dayEmotion;
        }).collect(Collectors.toList());

        emotionData.setData(dayEmotions);
        return emotionData;
    }
}
